package com.woniu.venue.controller;


import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.Dict;
import com.github.pagehelper.PageInfo;
import com.woniu.exception.venueNotice.VenueNoticeException;
import com.woniu.exception.venueNotice.VenueNoticeExceptionCode;
import com.woniu.model.Result;
import com.woniu.util.*;
import com.woniu.venue.dto.venueAdmin.VenueAdminDto;
import com.woniu.venue.dto.venueNotice.VenueNoticeDto;
import com.woniu.venue.form.venueNotice.AddNoticeFrom;
import com.woniu.venue.form.venueNotice.SearchNoticeFrom;
import com.woniu.venue.form.venueNotice.UpdateNoticeFrom;
import com.woniu.venue.param.venueNotice.AddNoticeParam;
import com.woniu.venue.param.venueNotice.SearchNoticeParam;
import com.woniu.venue.param.venueNotice.UpdateNoticeParam;
import com.woniu.venue.service.VenueNoticeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.List;
import java.util.UUID;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 杜岷芫
 * @since 2024年07月12日
 */

@Api(tags = "场馆公告")
@RestController
@RequestMapping("/venueNotice")
public class VenueNoticeController {

    @Autowired
    private VenueNoticeService venueNoticeService;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private OSSUtil ossUtil;

    @ApiOperation("修改公告信息")
    @PostMapping("/update")
    public Result update(@RequestBody UpdateNoticeFrom from) throws Exception {
        VenueNoticeDto venueNoticeDto= venueNoticeService.updateNotice(BeanUtil.toBean(from, UpdateNoticeParam.class));
        return Result.getResult().setMsg("成功").setData(venueNoticeDto);
    }

    @ApiOperation("新增公告信息")
    @PostMapping("/add")
    public Result addNotice(@RequestBody AddNoticeFrom from) throws Exception {
        AddNoticeParam param = BeanUtil.toBean(from, AddNoticeParam.class);
        VenueNoticeDto venueNoticeDto=  venueNoticeService.addNotice(param);
        return Result.getResult().setMsg("新增公告成功").setData(venueNoticeDto);
    }

    @ApiOperation("删除")
    @GetMapping("/delete/{id}")
    public Result deleteNotice(@PathVariable("id") Integer id) throws Exception {
        venueNoticeService.deleteNotice(id);
        return Result.getResult().setMsg("成功");
    }

    @ApiOperation("加载所有公告信息")
    @PostMapping("/search")
    public Result searchNotice(@RequestBody SearchNoticeFrom from) throws Exception {
        PageInfo<VenueNoticeDto> pageInfo = venueNoticeService.searchNotice(BeanUtil.toBean(from, SearchNoticeParam.class));
        return Result.getResult().setData(pageInfo).setMsg("成功");
    }

    /**
     * 教练查看公告信息
     *
     * @return
     * @throws Exception
     */
    @ApiOperation("最新公告")
    @GetMapping("/loadForUser")
    public Result loadForUser() throws Exception {
        List<VenueNoticeDto> venueNoticeDtos = venueNoticeService.loadForUser();
        return Result.getResult().setMsg("成功");
    }


    @ApiOperation("展示公告信息详情")
    @GetMapping("/showNotice/{id}")
    public Result showNotice(@PathVariable("id") Integer id) throws Exception {
        VenueNoticeDto venueNoticeDto= venueNoticeService.showNotice(id);
        return Result.getResult().setData(venueNoticeDto).setMsg("成功");
    }

    @PostMapping("/upload")
    public Result uploadNoticeImage(MultipartFile image) throws Exception {
        //1.读取image中的文件内容
        InputStream inputStream = image.getInputStream();
        byte[] header = new byte[8];
        inputStream.read(header, 0, 8);
        //2.判断该文件的类型是否为图片
        if (!(FileUtil.isJpg(header) || FileUtil.isPng(header))) {
            throw new VenueNoticeException(500,"上传图片失败");
        }
        //3.生成新的图片名称
        String prefix = UUID.randomUUID().toString().replaceAll("-", "");
        String fileName = prefix + "." + (FileUtil.isJpg(header) ? "jpg" : "png");
        //4.调用API上传
        String bookPath = ossUtil.uploadFile(image.getInputStream(), fileName,"dmy001234");
        return Result.getResult().setMsg("上传公告封面成功").setData(bookPath);
    }

    @PostMapping("/editorUpload")
    public Dict editorUpload(MultipartFile file) throws Exception {
        //1.读取image中的文件内容
        InputStream inputStream = file.getInputStream();
        byte[] header = new byte[8];
        inputStream.read(header, 0, 8);
        //2.判断该文件的类型是否为图片
        if (!(FileUtil.isJpg(header) || FileUtil.isPng(header))) {
            throw new VenueNoticeException(500,"上传图片失败");
        }
        //3.生成新的图片名称
        String prefix = UUID.randomUUID().toString().replaceAll("-", "");
        String fileName = prefix + "." + (FileUtil.isJpg(header) ? "jpg" : "png");
        //4.调用API上传
        String imgPath = ossUtil.uploadFile(file.getInputStream(), fileName,"dmy001234");
        return Dict.create().set("errno",0).set("data", CollUtil.newArrayList(Dict.create().set("url",imgPath)));
    }
}

